#pragma once

#include <glm\glm.hpp>
#include <vector>
#include "Buffer.h"

typedef unsigned short index_t;

class IndexBuffer: public Buffer
{
	index_t* m_Indices;
	unsigned int m_IndexCount;

	int updateBuffer();
	void releaseBuffer();

#ifdef _OPENGL
	struct BufferInfo {
		GLuint indexVBOID;
	};
#endif

#ifdef _DIRECTX
	struct BufferInfo {

	};
#endif

	BufferInfo m_BufferInfo;

public:
/*
	IndexBuffer(std::vector<index_t>& indices, unsigned int usageFlags = 0): Buffer(usageFlags) {
		m_Indices = indices.data();
		m_IndexCount = indices.size();
		updateBuffer();
	}
*/
	IndexBuffer(index_t* indices, int indexCount, unsigned int usageFlags = 0): Buffer(usageFlags) {
		m_Indices = indices;
		m_IndexCount = indexCount;
		updateBuffer();
	}

	virtual ~IndexBuffer(void) {
		releaseBuffer();

		if(m_Indices != NULL)
			delete[] m_Indices;
	}

	glm::uvec3 getTriangle(int index) {
		return glm::uvec3(m_Indices[index*3], m_Indices[index*3+1], m_Indices[index*3+2]);
	}

	index_t* getIndices() {
		return m_Indices;
	}

	size_t getIndexCount() {
		return m_IndexCount;
	}

	size_t getTriangleCount() {
		return m_IndexCount/3;
	}

	size_t getDeviceSize() {
		return m_IndexCount*sizeof(index_t);
	}

	// API dependant
	// 0 if success, !=0 error
	int sendToPipeline();

	// API dependant
	// setup the pipeline for rendering using index buffers
	static void setupPipeline();
	static void releasePipeline();

	static size_t getBufferSizeFor(unsigned int indexCount) {
		return sizeof(index_t)*indexCount;
	}
};